<template>
<div>
  <h1>添加课程页面</h1>
  <van-cell-group>
    <van-field v-model="form.title" label="标题" placeholder="请输入标题" />
    <van-field v-model="form.pic" label="图片" placeholder="请输入图片" />
    <van-field v-model="form.descrip" label="描述" placeholder="请描述" />
    <select name="" v-model="form.teacher_id">
      选择讲师
      <option :value="i.id" v-for="i in tlist">{{i.name}}</option>
    </select>
  </van-cell-group>
  <van-field name="radio" label="状态">
    <template #input>
      <van-radio-group v-model="form.status" direction="horizontal">
        <van-radio name="1">免费</van-radio>
        <van-radio name="2">收费</van-radio>
      </van-radio-group>
    </template>
  </van-field>
  <reply :data="cates"/>
  动态参数
  <div v-if='params'>
    <van-field v-for="(v,k) in params" :label='v' v-model="values[v]"/>
  </div>
  <van-button @click='add'>添加</van-button>

</div>
</template>

<script>


import reply from "./reply";
import bus from "./bus";


export default {
  name: "tclass",
  components:{'reply':reply},
  data(){
    return{
      tlist:[],
      form:{pid:0,params:'',status:'2'},
      values:{},
      cates:{},
      params:{},

    }
  },
  methods:{
    add_t(){
      this.$axios.get('users/classt')
      .then(i=>{
        this.tlist = i.data.list
      })
    },
    getlist(){
      this.$axios.get('users/cateM').then(res=>{
        this.cates={'id':0,'name':'sdfdsf','son':res.data.list}
      })
    },
    add(){
      if(this.values){
        this.form.params = JSON.stringify(this.values)
      }
      this.$axios.post('users/courseM',this.form).then(res=>{
      })
    }

  },
  mounted() {
    this.add_t()
    this.getlist()
    //监听
    bus.$on('pid',val=>{
      this.form.pid = val
      // 通过pid查询分类params
      console.log(val)
      this.$axios.get('users/getCateByid?id='+val).then(res=>{
        if(res.data.params){
          this.params = JSON.parse(res.data.params)
        }else{
          this.params = {}
        }
      })
    })
  }
}
</script>

<style scoped>

</style>
